weather_df =
rnoaa::meteo_pull_monitors(
c("USW00094728", "USW00022534", "USS0023B17S"),
var = c("PRCP", "TMIN", "TMAX"),
date_min = "2021-01-01",
date_max = "2022-12-31") |>
mutate(
name = recode(
id,
USW00094728 = "CentralPark_NY",
USW00022534 = "Molokai_HI",
USS0023B17S = "Waterhole_WA"),
tmin = tmin / 10,
tmax = tmax / 10) |>
select(name, id, everything())
## using cached file: /Users/taa-ra/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USW00094728.dly
## date created (size, mb): 2023-10-02 22:05:34.479049 (8.525)
## file min/max dates: 1869-01-01 / 2023-09-30
## using cached file: /Users/taa-ra/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USW00022534.dly
## date created (size, mb): 2023-10-02 22:05:46.647914 (3.83)
## file min/max dates: 1949-10-01 / 2023-09-30
## using cached file: /Users/taa-ra/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USS0023B17S.dly
## date created (size, mb): 2023-10-02 22:05:50.45828 (0.994)
## file min/max dates: 1999-09-01 / 2023-09-30
weather_df
## # A tibble: 2,190 × 6
## name id date prcp tmax tmin
## <chr> <chr> <date> <dbl> <dbl> <dbl>
## 1 CentralPark_NY USW00094728 2021-01-01 157 4.4 0.6
## 2 CentralPark_NY USW00094728 2021-01-02 13 10.6 2.2
## 3 CentralPark_NY USW00094728 2021-01-03 56 3.3 1.1
## 4 CentralPark_NY USW00094728 2021-01-04 5 6.1 1.7
## 5 CentralPark_NY USW00094728 2021-01-05 0 5.6 2.2
## 6 CentralPark_NY USW00094728 2021-01-06 0 5 1.1
## 7 CentralPark_NY USW00094728 2021-01-07 0 5 -1
## 8 CentralPark_NY USW00094728 2021-01-08 0 2.8 -2.7
## 9 CentralPark_NY USW00094728 2021-01-09 0 2.8 -4.3
## 10 CentralPark_NY USW00094728 2021-01-10 0 5 -1.6
## # ℹ 2,180 more rows
Basic scatterplot
ggplot(weather_df, aes(x = tmin, y = tmax)) + geom_point()
## Warning: Removed 17 rows containing missing values (`geom_point()`).
#plot of dataframe
weather_df |>
ggplot(aes(x = tmin, y = tmax)) +
geom_point()
## Warning: Removed 17 rows containing missing values (`geom_point()`).
#or
ggp_weather =
weather_df |>
ggplot(aes(x = tmin, y = tmax))
ggp_weather + geom_point()
## Warning: Removed 17 rows containing missing values (`geom_point()`).
Advanced scatterplot
ggplot(weather_df, aes(x = tmin, y = tmax)) +
geom_point(aes(color = name))
## Warning: Removed 17 rows containing missing values (`geom_point()`).
#or
ggplot(weather_df, aes(x = tmin, y = tmax)) +
geom_point(aes(color = name), alpha = .5) +
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 17 rows containing non-finite values (`stat_smooth()`).
## Removed 17 rows containing missing values (`geom_point()`).
#seprated
ggplot(weather_df, aes(x = tmin, y = tmax, color = name)) +
geom_point(alpha = .5) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite values (`stat_smooth()`).
## Removed 17 rows containing missing values (`geom_point()`).
#or
ggplot(weather_df, aes(x = date, y = tmax, color = name)) +
geom_point(aes(size = prcp), alpha = .5) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 19 rows containing missing values (`geom_point()`).
learning assessment
weather_df |>
filter(name == "CentralPark_NY") |>
mutate(
tmax_fahr = tmax * (9 / 5) + 32,
tmin_fahr = tmin * (9 / 5) + 32) |>
ggplot(aes(x = tmin_fahr, y = tmax_fahr)) +
geom_point(alpha = .5) +
geom_smooth(method = "lm", se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'
Odds and ends
ggplot(weather_df, aes(x = date, y = tmax, color = name)) +
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite values (`stat_smooth()`).
ggplot(weather_df, aes(x = tmax, y = tmin)) +
geom_hex()
## Warning: Removed 17 rows containing non-finite values (`stat_binhex()`).
Univariate plots
ggplot(weather_df, aes(x = tmax)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 17 rows containing non-finite values (`stat_bin()`).
#or
ggplot(weather_df, aes(x = tmax, fill = name)) +
geom_histogram(position = "dodge", binwidth = 2)
## Warning: Removed 17 rows containing non-finite values (`stat_bin()`).
#or
ggplot(weather_df, aes(x = tmax, fill = name)) +
geom_density(alpha = .4, adjust = .5, color = "blue")
## Warning: Removed 17 rows containing non-finite values (`stat_density()`).
box plots
ggplot(weather_df, aes(x = name, y = tmax)) + geom_boxplot()
## Warning: Removed 17 rows containing non-finite values (`stat_boxplot()`).
#or
ggplot(weather_df, aes(x = name, y = tmax)) +
geom_violin(aes(fill = name), alpha = .5) +
stat_summary(fun = "median", color = "blue")
## Warning: Removed 17 rows containing non-finite values (`stat_ydensity()`).
## Warning: Removed 17 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 3 rows containing missing values (`geom_segment()`).
#or
ggplot(weather_df, aes(x = tmax, y = name)) +
geom_density_ridges(scale = .85)
## Picking joint bandwidth of 1.54
## Warning: Removed 17 rows containing non-finite values
## (`stat_density_ridges()`).
learning assessment 2
ggplot(weather_df, aes(x = prcp)) +
geom_density(aes(fill = name), alpha = .5)
## Warning: Removed 15 rows containing non-finite values (`stat_density()`).
ggplot(weather_df, aes(x = prcp, y = name)) +
geom_density_ridges(scale = .85)
## Picking joint bandwidth of 9.22
## Warning: Removed 15 rows containing non-finite values
## (`stat_density_ridges()`).
#box plot
ggplot(weather_df, aes(y = prcp, x = name)) +
geom_boxplot()
## Warning: Removed 15 rows containing non-finite values (`stat_boxplot()`).
weather_df |>
filter(prcp > 0) |>
ggplot(aes(x = prcp, y = name)) +
geom_density_ridges(scale = .85)
## Picking joint bandwidth of 20.6
Saving and embedding plots
ggp_weather =
ggplot(weather_df, aes(x = tmin, y = tmax)) +
geom_point(aes(color = name), alpha = .5)
ggsave("ggp_weather.pdf", ggp_weather, width = 8, height = 5)
## Warning: Removed 17 rows containing missing values (`geom_point()`).
knitr::opts_chunk$set(
fig.width = 6,
fig.asp = .6,
out.width = "90%"
)
ggplot(weather_df, aes(x = tmin, y = tmax)) +
geom_point(aes(color = name))
## Warning: Removed 17 rows containing missing values (`geom_point()`).